﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using YZQ_DotNetArx.CAD;

namespace YZQ_DotNetArx.Test
{
    public static class 测试取样圆弧点
    {
        [CommandMethod("QY")]
        public static void QY()
        {
            PromptEntityOptions peo = new PromptEntityOptions("\n请选择多段线");
            peo.AllowNone = true;
            peo.SetRejectMessage("\n您选择的不是多段线,请重新选择...");
            peo.AddAllowedClass(typeof(Polyline), false);
            PromptEntityResult per = Env.Editor.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            Polyline pline = new Polyline();
            using (Transaction trans = Env.Database.TransactionManager.StartTransaction())
            {
                pline = trans.GetObject(per.ObjectId, OpenMode.ForWrite) as Polyline;
                List<Point3d> pts = new List<Point3d>();
                //遍历多段线的所有段
                for (int i = 0; i < pline.NumberOfVertices; i++)
                {
                    Point3d startPt = pline.GetPoint3dAt(i);
                    Point3d endPt = new Point3d();
                    if (i == pline.NumberOfVertices - 1)
                    {
                        endPt = pline.GetPoint3dAt(0);
                    }
                    else
                    {
                        endPt = pline.GetPoint3dAt(i+1);
                    }
                    //如果当前段是圆弧，则转换成多线段
                    if (pline.GetSegmentType(i) == SegmentType.Arc)
                    {
                        CircularArc3d arcSeg = pline.GetArcSegmentAt(i);
                        Arc arc = new Arc();
                        arc.CreateArc(endPt, arcSeg.Center, arcSeg.EndAngle);
                        arc.ReverseCurve();
                        //计算圆弧的长度
                        double dist = arc.GetDistanceAtParameter(arc.EndParam);
                        double sepDist = 0.06;
                        int n = Convert.ToInt32((dist / sepDist));
                        for (int j = 0; j < n; j++)
                        {
                            pts.Add(arc.GetPointAtDist(sepDist * j));
                        }
                    }
                    else if (pline.GetSegmentType(i) == SegmentType.Line)
                    {
                        pts.Add(startPt);
                    }
                }
                Polyline newPline = new Polyline();
                newPline.CreatePolyline(pts.ToArray());
                newPline.Closed = true;
                newPline.ColorIndex = 1;
                Env.AddEntityToModelSpace(newPline);
                trans.Commit();
                string roomTemp="";
                foreach (var item in pts)
                {
                    roomTemp += item.X.ToString() + "," + item.Y.ToString() + "," + "0"+",";
                }
                roomTemp += pts[0].X.ToString() + pts[0].Y.ToString() + "0";
                File.WriteAllLines("C:\\Users\\10635\\Desktop\\1.txt", new string []{ roomTemp });
            }

        }

    }
}
